import { reqGoodsDetail } from "@/api"

const state = {
  detailInfo: {}
}
const mutations = {
  SAVE_DETAILINFO(state, detailInfo) {
    state.detailInfo = detailInfo
  }
}
const actions = {
  async getDetailInfo({ commit }, skuId) {
    let result = await reqGoodsDetail(skuId);
    if (result && result.code == 200) {
      commit('SAVE_DETAILINFO', result.data);
    } else {
      console.error('获取商品详情失败')
    }
  }
}

const getters = {
  // 面包屑数据
  categoryView(state) {
    return state.detailInfo.categoryView || {} // 防止假报错
  },
  // 商品信息数据
  skuInfo(state) {
    return state.detailInfo.skuInfo || {} // 防止假报错
  },
  // 销售属性数据
  spuSaleAttrList(state) {
    return state.detailInfo.spuSaleAttrList || [] // 防止假报错
  },
  // 图片列表
  skuImageList(state) {
    if (state.detailInfo.skuInfo && state.detailInfo.skuInfo.skuImageList) {
      return state.detailInfo.skuInfo.skuImageList
    } else {
      return []
    }
  }
}

export default {
  namespaced: true, // 开启命名空间
  state,
  mutations,
  actions,
  getters
}


